package com.smalls.newvideotwo.utils;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;

public class DateUtil {
    private static final long ONE_DAY = 86400000;
    private static final long ONE_HOUR = 3600000;
    private static final long ONE_MINUTE = 60000;
    private static final long ONE_SECOND = 1000;
    public static String[] WEEK = new String[]{ "天", "一", "二", "三", "四", "五", "六" };

    public static Date string2Date(String str, String format) {
        try {
            return new SimpleDateFormat(format).parse(str);
        } catch (ParseException e) {
            e.printStackTrace();
            return new Date();
        }
    }

    public static String date2String(long time, String format) {
        return new SimpleDateFormat(format).format(Long.valueOf(time));
    }

    public static long getYearMonthDay(long date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(date);
        calendar.set(Calendar.HOUR_OF_DAY, 0);
        calendar.set(Calendar.SECOND, 0);
        calendar.set(Calendar.MINUTE, 0);
        calendar.set(Calendar.MILLISECOND, 0);
        return calendar.getTimeInMillis();
    }

    public static String getTimestampString(Date date) {
        long splitTime = new Date().getTime() - date.getTime();
        if (splitTime >= 2592000000L) {
            return new SimpleDateFormat("M月d日 HH:mm", Locale.CHINA).format(date);
        }
        if (splitTime < ONE_MINUTE) {
            return "刚刚";
        }
        if (splitTime < ONE_HOUR) {
            return String.format("%d分钟前", new Object[]{Long.valueOf(splitTime / ONE_MINUTE)});
        } else if (splitTime < ONE_DAY) {
            return String.format("%d小时前", new Object[]{Long.valueOf(splitTime / ONE_HOUR)});
        } else {
            return String.format("%d天前", new Object[]{Long.valueOf(splitTime / ONE_DAY)});
        }
    }

    public static String time24To12(String time) {
        String sx;
        String[] str = time.split(":");
        int h = Integer.valueOf(str[0]).intValue();
        int m = Integer.valueOf(str[1]).intValue();
        if (h < 1) {
            h = 12;
            sx = "上午";
        } else if (h < 12) {
            sx = "上午";
        } else if (h < 13) {
            sx = "下午";
        } else {
            sx = "下午";
            h -= 12;
        }
        return String.format("%d:%02d%s", new Object[]{Integer.valueOf(h), Integer.valueOf(m), sx});
    }

    public static String date2HH(Date date) {
        return new SimpleDateFormat("HH").format(date);
    }

    public static String date2HHmm(Date date) {
        return new SimpleDateFormat("HH:mm").format(date);
    }

    public static String date2HHmmss(Date date) {
        return new SimpleDateFormat("HH:mm:ss").format(date);
    }

    public static String date2MMdd(Date date) {
        return new SimpleDateFormat("MM.dd").format(date);
    }

    public static String date2yyyyMMdd(Date date) {
        return new SimpleDateFormat("yyyy.MM.dd").format(date);
    }

    public static String date2MMddWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return new SimpleDateFormat("MM月dd日 星期").format(date) + WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static String date2yyyyMMddWeek(Date date) {
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        return new SimpleDateFormat("yyyy年MM月dd日 星期").format(date) + WEEK[calendar.get(Calendar.DAY_OF_WEEK) - 1];
    }

    public static Date getDateBefore(Date date, int n) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH) - n);
        return now.getTime();
    }

    public static Date getDateAfter(Date date, int day) {
        Calendar now = Calendar.getInstance();
        now.setTime(date);
        now.set(Calendar.DAY_OF_MONTH, now.get(Calendar.DAY_OF_MONTH) + day);
        return now.getTime();
    }

    public static String ConverToString(Date date) {
        return new SimpleDateFormat("yyyy-MM-dd").format(date);
    }

    public static Date ConverToDate(String strDate) throws Exception {
        return new SimpleDateFormat("yyyy-MM-dd").parse(strDate);
    }
}
